package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzCourse;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzSchoolArchives;
import com.zzedu.domain.ZzUploadFile;
import com.zzedu.dto.ZzSchoolArchivesDTO;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.mapper.ZzSchoolArchivesMapper;
import com.zzedu.service.IZzCourseService;
import com.zzedu.service.IZzSchoolArchivesService;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.service.IZzUploadFileService;
import com.zzedu.vo.ZzSchoolArchivesVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学校资料Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-06-18
 */
@Service
public class ZzSchoolArchivesServiceImpl extends ServiceImpl<ZzSchoolArchivesMapper, ZzSchoolArchives> implements IZzSchoolArchivesService {

    @Autowired
    IZzSchoolService zzSchoolService;
    @Autowired
    IZzUploadFileService zzUploadFileService;
    @Autowired
    IOssClientService ossClientService;

    @Autowired
    IZzCourseService zzCourseService;
    @Override
    public LambdaQueryWrapper<ZzSchoolArchives> buildQuery(ZzSchoolArchivesDTO zzSchoolArchives) {
        LambdaQueryWrapper<ZzSchoolArchives> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzSchoolArchives.getName()), ZzSchoolArchives::getName, zzSchoolArchives.getName());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzSchoolArchives.getRemark()), ZzSchoolArchives::getRemark, zzSchoolArchives.getRemark());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzSchoolArchives.getType()), ZzSchoolArchives::getType, zzSchoolArchives.getType());
        lambdaQueryWrapper.eq(null != zzSchoolArchives.getSchoolId(), ZzSchoolArchives::getSchoolId, zzSchoolArchives.getSchoolId());
        lambdaQueryWrapper.eq(null != zzSchoolArchives.getFileId(), ZzSchoolArchives::getFileId, zzSchoolArchives.getFileId());
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(zzSchoolArchives.getUnitIdArray()), ZzSchoolArchives::getUnitId, zzSchoolArchives.getUnitIdArray());
        lambdaQueryWrapper.orderByDesc(ZzSchoolArchives::getId);
        return lambdaQueryWrapper;
    }

    public List<ZzSchoolArchivesVO> convertVO(List<ZzSchoolArchives> zzSchoolArchivesList) {
        List<ZzSchoolArchivesVO> zzSchoolArchivesVOS = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(zzSchoolArchivesList)) {
            for (ZzSchoolArchives zzSchoolArchives : zzSchoolArchivesList) {
                ZzSchoolArchivesVO zzSchoolArchivesVO = new ZzSchoolArchivesVO();
                BeanUtils.copyBeanProp(zzSchoolArchivesVO, zzSchoolArchives);
                if (null != zzSchoolArchives.getSchoolId()) {
                    ZzSchool zzSchool = this.zzSchoolService.getById(zzSchoolArchives.getSchoolId());
                    if (null != zzSchool) {
                        zzSchoolArchivesVO.setSchoolName(zzSchool.getSchoolName());
                    }
                }
                if (null != zzSchoolArchives.getFileId()) {
                    ZzUploadFile zzUploadFile = this.zzUploadFileService.getById(zzSchoolArchives.getFileId());
                    if (null != zzUploadFile) {
                        zzSchoolArchivesVO.setUrl(zzUploadFile.getUrl());
                        zzSchoolArchivesVO.setFileName(zzUploadFile.getOldFileName());
                    }
                }
                if(null!=zzSchoolArchives.getUnitId()){
                    ZzCourse zzCourse = zzCourseService.getById(zzSchoolArchives.getUnitId());
                    if(null!=zzCourse){
                        zzSchoolArchivesVO.setUnitName(zzCourse.getName());
                    }
                }
                zzSchoolArchivesVOS.add(zzSchoolArchivesVO);
            }
        }
        return zzSchoolArchivesVOS;
    }
}